package com.uenpay.im.ui.old.activity.movement;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.entities.PersonInfoConstant;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @created 2016-1-21 上午11:43:54
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class EditMovementActivity extends BaseActivity {

    private short update_type;
    private EditText editt_movement_et;
    private String oldStr;
    private TextView titleTv, charge_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_edit_movement_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
    }

    protected void initView() {
        titleTv = (TextView) findViewById(R.id.title_textview);
        charge_tv = (TextView) findViewById(R.id.charge_tv);
        update_type = getIntent().getShortExtra("update_type",
                PersonInfoConstant.updateNothing);
        oldStr = getIntent().getStringExtra("oldStr");
        editt_movement_et = (EditText) findViewById(R.id.editt_movement_et);
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.save));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(oldStr)) {
            editt_movement_et.setText(oldStr);
            editt_movement_et.setSelection(oldStr.length());
        }
        if (update_type == UpdateMovementField.UPDATE_MOVEMENT_TITLE) {
            titleTv.setText(getResources().getString(
                    R.string.movement_publish_title));
        } else if (update_type == UpdateMovementField.UPDATE_MOVEMENT_NUM) {
            titleTv.setText(getResources().getString(
                    R.string.movement_publish_num));
            editt_movement_et.setInputType(InputType.TYPE_CLASS_PHONE);
        } else if (update_type == UpdateMovementField.UPDATE_MOVEMENT_SPECIFIC_ADDRESS) {
            titleTv.setText(getResources().getString(
                    R.string.movement_specific_address));
        } else if (update_type == UpdateMovementField.UPDATE_MOVEMENT_COST) {
            titleTv.setText(getResources().getString(
                    R.string.movement_publish_cost));
            editt_movement_et.setInputType(InputType.TYPE_CLASS_PHONE);
        } else if (update_type == UpdateMovementField.UPDATE_MOVEMENT_INTRO) {
            titleTv.setText(getResources().getString(
                    R.string.movement_publish_introduce));
        }

        editt_movement_et.setSelection(editt_movement_et.getText().toString()
                .length());
        Utils.showSoftKeyboard(editt_movement_et);
    }

    protected void initListener() {
        findViewById(R.id.back_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Utils.hideSoftKeyboard(editt_movement_et);
                        finish();
                    }
                });
        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        String result = editt_movement_et.getText().toString();
                        if (update_type == UpdateMovementField.UPDATE_MOVEMENT_COST) {
                            if (result.startsWith(".") || result.endsWith(".") || result.indexOf("+") > -1
                                    || result.indexOf("-") > -1
                                    || result.indexOf("*") > -1
                                    || result.indexOf("/") > -1) {
                                IMApplication.showToast(R.string.movement_error_cost);
                                return;
                            }
                        }

                        if (result.indexOf(".") > -1) {
                            String[] str = result.split("\\.");
                            if (str.length >= 3) {
                                IMApplication.showToast(R.string.movement_error_cost);
                                return;
                            }
                        }
                        if (!TextUtils.isEmpty(result)) {
                            Intent intent = new Intent();
                            intent.putExtra("result", result);
                            setResult(RESULT_OK, intent);
                            Utils.hideSoftKeyboard(editt_movement_et);
                            finish();
                        } else {
                            if (update_type == UpdateMovementField.UPDATE_MOVEMENT_COST) {
                                Intent intent = new Intent();
                                intent.putExtra("result", "0");
                                setResult(RESULT_OK, intent);
                                Utils.hideSoftKeyboard(editt_movement_et);
                                finish();
                            }
                        }
                    }
                });
        editt_movement_et.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                if (update_type == UpdateMovementField.UPDATE_MOVEMENT_COST) {
                    String cost = arg0.toString();
                    if (TextUtils.isEmpty(cost)) {
                        charge_tv.setVisibility(View.VISIBLE);
                    } else {
                        charge_tv.setVisibility(View.GONE);
                    }
                }

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {

            }
        });
    }
}
